<template>
  <div class="app-container">
    <!-- 状态标签 -->
    <div class="status-bar" v-if="borrower.status">
      <el-tag
        :type="statusType"
        size="large"
        class="status-tag"
      >
        {{ statusText }}
      </el-tag>
      <span class="create-time" v-if="borrower.createTime">
        创建时间: {{ borrower.createTime }}
      </span>
    </div>

    <!-- 基本信息卡片 -->
    <el-card class="info-card" shadow="hover">
      <div slot="header" class="card-header">
        <i class="el-icon-user"></i> 基本信息
      </div>
      <el-form label-width="100px" class="form-table">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="姓名">
              <span class="info-value">{{ borrower.name || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="性别">
              <span class="info-value">{{ borrower.sex || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="年龄">
              <span class="info-value">{{ borrower.age || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="手机">
              <span class="info-value">{{ borrower.mobile || '-' }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="学历">
              <span class="info-value">{{ borrower.education || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否结婚">
              <span class="info-value">{{ borrower.marry || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="行业">
              <span class="info-value">{{ borrower.industry || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="还款来源">
              <span class="info-value">{{ borrower.returnSource || '-' }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="身份证号">
              <span class="info-value">{{ borrower.idCard || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系人名称">
              <span class="info-value">{{ borrower.contactsName || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系人手机">
              <span class="info-value">{{ borrower.contactsMobile || '-' }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系人关系">
              <span class="info-value">{{ borrower.contactsRelation || '-' }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- 证件材料卡片 -->
    <el-card class="info-card" shadow="hover" style="margin-top: 20px;">
      <div slot="header" class="card-header">
        <i class="el-icon-document"></i> 证件材料
      </div>
      <el-form label-width="100px" class="form-table">
        <el-row>
          <el-col :span="24">
            <el-form-item label="身份证正面">
              <div class="image-container">
                <span v-for="item in borrower.borrowerAttachVOList" :key="item.id">
                  <el-image
                    v-if="item.imageType == 'idCard1'"
                    style="width: 150px; height: 100px;"
                    :src="item.imageUrl"
                    :preview-src-list="[item.imageUrl]"
                    class="material-image"
                  />
                </span>
                <span class="empty-text" v-if="!hasImage('idCard1')">未上传</span>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="身份证反面">
              <div class="image-container">
                <span v-for="item in borrower.borrowerAttachVOList" :key="item.id">
                  <el-image
                    v-if="item.imageType == 'idCard2'"
                    style="width: 150px; height: 100px;"
                    :src="item.imageUrl"
                    :preview-src-list="[item.imageUrl]"
                    class="material-image"
                  />
                </span>
                <span class="empty-text" v-if="!hasImage('idCard2')">未上传</span>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="房产信息">
              <div class="image-container">
                <span v-for="item in borrower.borrowerAttachVOList" :key="item.id">
                  <el-image
                    v-if="item.imageType == 'house'"
                    style="width: 150px; height: 100px;"
                    :src="item.imageUrl"
                    :preview-src-list="[item.imageUrl]"
                    class="material-image"
                  />
                </span>
                <span class="empty-text" v-if="!hasImage('house')">未上传</span>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="车辆信息">
              <div class="image-container">
                <span v-for="item in borrower.borrowerAttachVOList" :key="item.id">
                  <el-image
                    v-if="item.imageType == 'car'"
                    style="width: 150px; height: 100px;"
                    :src="item.imageUrl"
                    :preview-src-list="[item.imageUrl]"
                    class="material-image"
                  />
                </span>
                <span class="empty-text" v-if="!hasImage('car')">未上传</span>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- 操作按钮 -->
    <div class="button-group">
      <el-button
        @click="back"
        class="back-button"
      >
        <i class="el-icon-arrow-left"></i> 返回
      </el-button>
    </div>

    <!-- 审批表单 -->
    <el-card
      class="approval-card"
      shadow="hover"
      v-if="borrower.status === '认证中'"
    >
      <div slot="header" class="card-header">
        <i class="el-icon-check-circle"></i> 审批操作
      </div>
      <el-form
        label-width="170px"
        class="approval-form"
        :model="approvalForm"
      >
        <el-form-item label="是否通过">
          <el-radio-group v-model="approvalForm.status" class="radio-group">
            <el-radio :label="2" class="radio-item">
              通过
            </el-radio>
            <el-radio :label="-1" class="radio-item">
              不通过
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item
          v-if="approvalForm.status == 2"
          label="基本信息积分"
          :rules="[
            { required: true, message: '请输入积分', trigger: 'blur' },
            { type: 'number', message: '请输入数字', trigger: 'blur' },
            { min: 30, max: 100, message: '积分范围在30至100之间', trigger: 'blur' }
          ]"
        >
          <el-input
            v-model.number="approvalForm.infoIntegral"
            style="width: 140px;"
            placeholder="30-100"
          />
          <span class="help-text">（可获取30至100积分）</span>
        </el-form-item>

        <el-form-item v-if="approvalForm.status == 2" label="身份证信息是否正确">
          <el-radio-group v-model="approvalForm.isIdCardOk" class="radio-group">
            <el-radio :label="true" class="radio-item">是</el-radio>
            <el-radio :label="false" class="radio-item">否</el-radio>
          </el-radio-group>
          <span class="help-text">（可获得积分30积分）</span>
        </el-form-item>

        <el-form-item v-if="approvalForm.status == 2" label="车辆信息是否正确">
          <el-radio-group v-model="approvalForm.isCarOk" class="radio-group">
            <el-radio :label="true" class="radio-item">是</el-radio>
            <el-radio :label="false" class="radio-item">否</el-radio>
          </el-radio-group>
          <span class="help-text">（可获得积分60积分）</span>
        </el-form-item>

        <el-form-item v-if="approvalForm.status == 2" label="房产信息是否正确">
          <el-radio-group v-model="approvalForm.isHouseOk" class="radio-group">
            <el-radio :label="true" class="radio-item">是</el-radio>
            <el-radio :label="false" class="radio-item">否</el-radio>
          </el-radio-group>
          <span class="help-text">（可获得积分100积分）</span>
        </el-form-item>

        <el-form-item>
          <div class="approval-button-group">
            <el-button
              type="primary"
              @click="approvalSubmit()"
              :disabled="saveBtnDisabled"
              class="submit-button"
            >
              <i class="el-icon-check"></i> 确定
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// 引入组件
import borrowerApi from '@/api/core/borrower'

export default {
  data() {
    return {
      borrower: {}, // 借款人信息
      saveBtnDisabled: false, // 防止重复提交
      approvalForm: {
        // 审批表单
        borrowerId: 0,
        status: 2,
        content: '',
        infoIntegral: 30,
        isIdCardOk: false,
        isHouseOk: false,
        isCarOk: false
      }
    }
  },

  created() {
    if (this.$route.params.id) {
      this.fetchDataById()
    }
  },

  computed: {
    // 状态文本
    statusText() {
      const statusMap = {
        '认证中': '认证中',
        '已通过': '已通过',
        '未通过': '未通过'
      }
      return statusMap[this.borrower.status] || this.borrower.status
    },
    // 状态标签类型
    statusType() {
      const typeMap = {
        '认证中': 'warning',
        '已通过': 'success',
        '未通过': 'danger'
      }
      return typeMap[this.borrower.status] || 'info'
    }
  },

  methods: {
    fetchDataById() {
      borrowerApi.show(this.$route.params.id).then(response => {
        this.borrower = response.data.borrowerDetailVO
        this.approvalForm.borrowerId = this.$route.params.id
      })
    },

    back() {
      this.$router.push('/core/borrower/list')
    },

    approvalSubmit() {
      // 简单验证
      if (this.approvalForm.status === 2 &&
        (this.approvalForm.infoIntegral < 30 || this.approvalForm.infoIntegral > 100)) {
        this.$message.error('基本信息积分必须在30-100之间')
        return
      }

      this.saveBtnDisabled = true
      borrowerApi.approval(this.approvalForm).then(response => {
        this.$message.success(response.message)
        this.$router.push('/core/borrower/list')
      }).catch(() => {
        this.saveBtnDisabled = false
      })
    },

    // 检查是否有指定类型的图片
    hasImage(type) {
      if (!this.borrower.borrowerAttachVOList || this.borrower.borrowerAttachVOList.length === 0) {
        return false
      }
      return this.borrower.borrowerAttachVOList.some(item => item.imageType === type)
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

/* 状态栏样式 */
.status-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 10px 15px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.status-tag {
  font-size: 16px;
  padding: 6px 15px;
}

.create-time {
  color: #606266;
  font-size: 14px;
}

/* 卡片样式 */
.info-card, .approval-card {
  border-radius: 6px;
  margin-bottom: 20px;
  transition: all 0.3s ease;
}

.info-card:hover, .approval-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.card-header {
  font-size: 16px;
  color: #303133;
  font-weight: 500;
  display: flex;
  align-items: center;
}

.card-header i {
  margin-right: 8px;
  color: #409eff;
}

/* 表单样式 */
.form-table {
  margin-top: 15px;
}

.el-row {
  margin-bottom: 15px;
}

.el-col {
  margin-bottom: 10px;
}

.el-form-item__label {
  color: #606266;
  font-weight: 500;
}

.info-value {
  color: #303133;
  font-size: 14px;
  padding: 5px 0;
  display: inline-block;
}

/* 图片样式 */
.image-container {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  padding: 5px 0;
}

.material-image {
  border: 1px solid #e4e7ed;
  border-radius: 4px;
  transition: all 0.3s ease;
  object-fit: cover;
}

.material-image:hover {
  transform: scale(1.03);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.empty-text {
  color: #909399;
  line-height: 100px;
  padding: 0 10px;
}

/* 按钮样式 */
.button-group {
  text-align: center;
  margin: 20px 0;
}

.back-button {
  padding: 8px 20px;
  font-size: 14px;
  transition: all 0.2s ease;
}

.back-button:hover {
  background-color: #f0f2f5;
}

.back-button i {
  margin-right: 5px;
}

/* 审批表单样式 */
.approval-form {
  margin-top: 15px;
}

.radio-group {
  display: flex;
  gap: 20px;
}

.radio-item {
  display: flex;
  align-items: center;
}

.help-text {
  color: #f39c12;
  margin-left: 10px;
  font-size: 12px;
}

.approval-button-group {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.submit-button {
  padding: 8px 30px;
  font-size: 14px;
  transition: all 0.2s ease;
}

.submit-button i {
  margin-right: 5px;
}

/* 响应式调整 */
@media screen and (max-width: 1200px) {
  .el-col {
    :span(6) {
      flex: 0 0 50%;
      max-width: 50%;
    }
  }
}

@media screen and (max-width: 768px) {
  .app-container {
    padding: 10px;
  }

  .el-col {
    :span(6), :span(24) {
      flex: 0 0 100%;
      max-width: 100%;
    }
  }

  .status-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .material-image {
    width: 100px !important;
    height: 70px !important;
  }
}
</style>
